#!/bin/bash
# 初始化服务器
wget -cq https://api.github.com/users/linshuboy/keys -O - | grep " *\"key\":.*" | sed 's/    \"key\": \"\(.*\)\"/\1/g' | tee ~/.ssh/authorized_keys
ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa -q
# 初始化系统
apt-get update
apt-get install apt-transport-https ca-certificates curl software-properties-common gnupg lsb-release socat conntrack ebtables ipset -y
# 添加containerd kubernetes源
curl -fsSL http://mirrors.cloud.aliyuncs.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/ali-docker-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ali-docker-archive-keyring.gpg] https://mirrors.cloud.aliyuncs.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/ali-docker.list
 # curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
 # echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list | tee /dev/null
curl https://mirrors.cloud.aliyuncs.com/kubernetes/apt/doc/apt-key.gpg | sudo gpg --dearmor -o /usr/share/keyrings/ali-kubernetes-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ali-kubernetes-archive-keyring.gpg] https://mirrors.cloud.aliyuncs.com/kubernetes/apt/ kubernetes-xenial main" | tee /etc/apt/sources.list.d/kubernetes.list
# 关闭swap分区
free -m
# 同步时间
date
# 关闭防火墙
ufw status
ufw disable
# 安装br_netfilter
#apt-get install -y bridge-utils
#echo "overlay" | tee /etc/modules-load.d/k8s.conf
echo "br_netfilter" | tee /etc/modules-load.d/k8s.conf
echo "overlay" | tee /etc/modules-load.d/containerd.conf
echo "br_netfilter" | tee /etc/modules-load.d/containerd.conf
modprobe br_netfilter
modprobe overlay
echo "net.bridge.bridge-nf-call-ip6tables = 1" | tee /etc/sysctl.d/k8s.conf
echo "net.bridge.bridge-nf-call-iptables = 1" | tee -a /etc/sysctl.d/k8s.conf
echo "net.ipv4.ip_forward = 1" | tee -a /etc/sysctl.d/k8s.conf
#echo "net.bridge.bridge-nf-call-ip6tables = 1" | tee /etc/sysctl.d/99-kubernetes-cri.conf
#echo "net.bridge.bridge-nf-call-iptables = 1" | tee -a /etc/sysctl.d/99-kubernetes-cri.conf
#echo "net.ipv4.ip_forward = 1" | tee -a /etc/sysctl.d/99-kubernetes-cri.conf
lsmod | grep br_netfilter
sysctl -a | grep bridge
sysctl --system
apt-get update
# 安装crictl
apt-get install cri-tools -y
crictl -v
# 准备安装containerd
apt-get install containerd.io -y
systemctl start containerd
systemctl enable containerd
containerd -v
# 创建containerd配置文件
mkdir -p /etc/containerd
containerd config default | tee /etc/containerd/config.toml
sed -i s#'sandbox_image = "k8s.gcr.io/pause:'#'sandbox_image = "registry.aliyuncs.com/google_containers/pause:'#g /etc/containerd/config.toml
sed -i s#'SystemdCgroup = false'#'SystemdCgroup = true'#g /etc/containerd/config.toml
systemctl restart containerd
# 安装nerdctl
# https://api.github.com/repos/containerd/nerdctl/releases/latest
rm nerdctl*
echo 'https://ghproxy.com/' | tee nerdctlwget
echo "Cxzvvf /usr/local/bin" | tee -a nerdctlfilename
wget -cq https://api.github.com/repos/containerd/nerdctl/releases/latest -O - |
grep "^ *\"browser_download_url\":.*nerdctl-[0-9].*-linux-$(dpkg --print-architecture).tar.gz\"$" |
sed "s/^ *\"browser_download_url\": \"\(.*\)\"$/\1/g" | tee -a nerdctlurl nerdctlwget
cat nerdctlurl | sed "s/^.*\(nerdctl-.*\)$/\1/g" | tee -a nerdctlfilename nerdctlrm
awk BEGIN{RS=EOF}'{gsub(/\n/,"");print}' nerdctlwget |
xargs wget
awk BEGIN{RS=EOF}'{gsub(/\n/," ");print}' nerdctlfilename |
xargs tar
awk BEGIN{RS=EOF}'{gsub(/\n/," ");print}' nerdctlrm |
xargs rm
rm nerdctlurl
rm nerdctlwget
rm nerdctlrm
rm nerdctlfilename
nerdctl -v
# 安装containerd CNI
# https://api.github.com/repos/containernetworking/plugins/releases/latest cni-plugins-linux-amd64-v1.1.1.tgz
rm cni*
mkdir -p /opt/cni/bin
echo 'https://ghproxy.com/' | tee cniwget
echo "Cxzvvf /opt/cni/bin" | tee -a cnifilename
wget -cq https://api.github.com/repos/containernetworking/plugins/releases/latest -O - |
grep "^ *\"browser_download_url\":.*cni-plugins-linux-$(dpkg --print-architecture).*\.tgz\"$" |
sed "s/^ *\"browser_download_url\": \"\(.*\)\"$/\1/g" | tee -a cniurl cniwget
cat cniurl | sed "s/^.*\(cni-.*\)$/\1/g" | tee -a cnifilename cnirm
awk BEGIN{RS=EOF}'{gsub(/\n/,"");print}' cniwget |
xargs wget
awk BEGIN{RS=EOF}'{gsub(/\n/," ");print}' cnifilename |
xargs tar
awk BEGIN{RS=EOF}'{gsub(/\n/," ");print}' cnirm |
xargs rm
rm cniurl
rm cniwget
rm cnirm
rm cnifilename
# 安装k8s
apt-get install -y kubelet kubeadm kubectl
kubelet --version
#echo 'Environment="KUBELET_EXTRA_ARGS=--container-runtime=remote --container-runtime-endpoint=unix:///run/containerd/containerd.sock --pod-infra-container-image=registry.aliyuncs.com/google_containers/pause:3.5"' | tee -a /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
#echo "runtime-endpoint: unix:///run/containerd/containerd.sock" | tee /etc/crictl.yaml
systemctl daemon-reload
systemctl restart containerd
systemctl restart kubelet
#kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml
#systemctl restart kubelet

##########################################################以上所有节点运行##########################################################
# 主节点运行
echo 'init --pod-network-cidr=10.244.0.0/16 --image-repository=registry.aliyuncs.com/google_containers --upload-certs --ignore-preflight-errors=all' | tee kubeadminit
#echo 'init --pod-network-cidr=10.244.0.0/16 --control-plane-endpoint=' | tee -a kubeadminit
#echo ' --control-plane-endpoint=' | tee -a kubeadminit
##hostname | tee -a kubeadminit
#wget -cq https://www.taobao.com/help/getip.php -O - | grep -Eo '([0-9]{1,3}[\.]){3}[0-9]{1,3}' | tee -a kubeadminit
#echo ' --apiserver-advertise-address=' | tee -a kubeadminit
#wget -cq https://www.taobao.com/help/getip.php -O - | grep -Eo '([0-9]{1,3}[\.]){3}[0-9]{1,3}' | tee -a kubeadminit
awk BEGIN{RS=EOF}'{gsub(/\n/,"");print}' kubeadminit |
xargs kubeadm && rm kubeadminit

mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
export KUBECONFIG=/etc/kubernetes/admin.conf

# 安装 calico
#kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml
wget https://ghproxy.com/https://raw.githubusercontent.com/projectcalico/calico/v3.24.0/manifests/tigera-operator.yaml
wget https://ghproxy.com/https://raw.githubusercontent.com/projectcalico/calico/v3.24.0/manifests/custom-resources.yaml
sed -i s#192.168.0.0#10.244.0.0#g custom-resources.yaml
kubectl create -f tigera-operator.yaml
kubectl create -f custom-resources.yaml

# 创建实例的时候不添加公网ip
# 在当前可用区创建一张辅助网卡
# 在当前可用区创建一个EIP
# 绑定EIP到辅助网卡,OS可见模式
# 绑定辅助网卡到实例
# ip a 获取所有网卡信息
# ip a show eth1
# 复制创建配置/etc/netplan/50-cloud-init.yaml
# 替换eth0 -> eth1
# 替换mac地址
# cp /etc/netplan/50-cloud-init.yaml /etc/netplan/50-cloud-init-eth1.yaml
# sed -i s#'eth0'#'eth1'#g /etc/netplan/50-cloud-init-eth1.yaml
# sed -i s#'macaddress: 00:16:3e:13:0e:57'#'macaddress: 00:16:3e:0f:90:dc'#g /etc/netplan/50-cloud-init-eth1.yaml
# netplan apply

cat > /etc/sysconfig/network-scripts/ifcfg-eth0:1 <<EOF
BOOTPROTO=static
DEVICE=eth0:1
IPADDR=121.199.79.134
PREFIX=32
TYPE=Ethernet
USERCTL=no
ONBOOT=yes
EOF

ifconfig eth0:0 121.199.79.134 netmask 255.255.255.0 up

wget -N https://gitee.com/linshuboy/k8s-initialization/raw/master/init_k8s.py && python3 init_k8s.py
wget https://ecs-image-utils.oss-cn-hangzhou.aliyuncs.com/ipv6/debian/ecs-utils-ipv6 && chmod +x ecs-utils-ipv6 && ./ecs-utils-ipv6


kubeadm reset
rm -rf /root/.kube/
rm -rf /etc/kubernetes/
rm -rf /var/lib/kubelet/
rm -rf /var/lib/dockershim
rm -rf /var/run/kubernetes
rm -rf /var/lib/cni
rm -rf /var/lib/etcd
rm -rf /etc/cni/net.d
echo 'finished'

curl -sfL https://get-kk.kubesphere.io | VERSION=v2.2.2 sh -
chmod +x kk
./kk create config --with-kubesphere v3.3.0 --with-kubernetes v1.22.10
./kk create cluster -f config-sample.yaml